//
//  FKHotBuinessViewModel.m
//  WanTao
//
//  Created by 刘文超 on 2019/8/26.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKHotBuinessViewModel.h"
@interface FKHotBuinessViewModel()
{
    CGFloat _oldY;
}
@end
@implementation FKHotBuinessViewModel
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray =@[].mutableCopy;
    }return _dataArray;
}
-(NSArray *)refreshArray
{
    if (!_refreshArray) {
        _refreshArray =@[];
    }return _refreshArray;
}
-(void)fk_loadData:(NSMutableDictionary *)param WithResult:(void (^) (id result))successResult
{

    [FKHttpTool postWithSuffix:@"m=Home&c=Item&a=themeActivity" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSArray *array = result.data[@"set_meal"];
            if ([fkFORMAT(@"%@",param[@"page"]) isEqualToString:@"1"]) {
                [self.dataArray removeAllObjects];
            }
            for (NSDictionary *dict in array) {
                [self.dataArray addObject:dict];
            }
            self.refreshArray = array;
            self.buttonImage = fkFORMAT(@"%@",result.data[@"suspension"][@"content"]) ;
            self.userHeadImage = fkFORMAT(@"%@",result.data[@"user"][@"avatar"]);
            successResult(result.data);
        }
    } failure:^(NSError *error) {
     
    }];
}
#pragma mark --delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return  self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static  NSString *cellID=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.backgroundColor = [UIColor clearColor];
        UIImageView *logoImageView=[[UIImageView alloc]init];
        [cell.contentView addSubview:logoImageView];
        logoImageView.tag = 100;
        [logoImageView fk_viewCornerRadius:10 borderWidth:0 borderColor:[UIColor clearColor]];
        [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.offset(0);
            make.left.offset(12);
            make.right.offset(-12);
        }];
        

    }
    UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:100];
    NSDictionary *dict = self.dataArray[indexPath.section];
    [imageView sd_setImageWithURL:[NSURL URLWithString:dict[@"gift_package_image"]] placeholderImage:kPlaceholder_1x1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.dataArray[indexPath.section];
    if (self.didItemBlock) {
        self.didItemBlock(fkFORMAT(@"%@",dict[@"id"]));
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
   
        if (scrollView.contentOffset.y > _oldY) {
            // 上滑
            if (self.scrollViewBlock) {
                self.scrollViewBlock(NO);
            }
           
        }
        else{
            // 下滑
            if (self.scrollViewBlock) {
                self.scrollViewBlock(YES);
            }
        }
  
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    // 获取开始拖拽时tableview偏移量
    _oldY = scrollView.contentOffset.y;
}
@end
